package com.zjz.service.user;

import com.zjz.dao.BaseDao;
import com.zjz.dao.user.UserDao;
import com.zjz.dao.user.UserDaoImpl;
import com.zjz.pojo.User;
import org.junit.Test;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class UserServiceImpl implements UserService{

    //业务层都会调用dao层，所以我们要引入dao层
    private UserDao userDao;
    public UserServiceImpl() {
        userDao = new UserDaoImpl();
    }
 // 登录
    public User login(String userCode, String password) {
        Connection connection = null;
        User user = null;
        try {
            connection = BaseDao.getConnection();
            //通过业务层调用具体的数据库操作
            user = userDao.getLoginUser(connection,userCode);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return user;
    }
// 修改密码
    public Boolean updatePsd(int id, String pwd) {
        Connection connection = null;
        boolean flag = false;
        try {
            connection = BaseDao.getConnection();
            if(userDao.updatePsd(connection,id,pwd)>0){
                flag = true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }

        return flag;
    }

// 分页查询数据
  // 查询记录数
    public int getUserCount(String username, int userRole) {

        Connection connection = null;
        int count = 0;

        try {
            connection = BaseDao.getConnection();
            count = userDao.getUserCount(connection,username,userRole);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return count;
    }

  // 通过条件查询-userList
    public List<User> getUserList(String querryUserName, int querryUserRole, int currentPageNo, int pageSize) {
        Connection connection = null;
        List<User> userList = null;
        try {
            connection = BaseDao.getConnection();
            //通过业务层调用具体的数据库操作
            userList = userDao.getUserList(connection,querryUserName,querryUserRole,currentPageNo,pageSize);
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return userList;
    }



/*

    @Test
    public void Test() {
        UserServiceImpl userService = new UserServiceImpl();
        List<User> userList = userService.getUserList(null, 1, 1, 5);
        for (User user : userList) {
            System.out.println(user);
        }
    }

*/


    /*  @Test
    public void Test(){
        UserServiceImpl userService = new UserServiceImpl();
        int userCount = userService.getUserCount(null, 0);
        System.out.println(userCount);
    }*/
}
